﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Text;

namespace HeadersModeConsume
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var channel = Common.RabbitMQ.GetChannel();
            CreateConsume(Convert.ToInt32(Console.ReadLine()), channel, Console.ReadLine());

            Console.ReadKey();
        }
        static void CreateConsume(int i, IModel channel, string match)
        {
            //交换机
            channel.ExchangeDeclare("myheadersexchange", ExchangeType.Headers, true, false, default);

            //队列
            channel.QueueDeclare($"myheadersqueue{i}", true, false, false, default);

            var map = new Dictionary<string, object>
            {
                {"x-match",match},
                {"name","abc"},
                {"age",20},
            };

            //绑定
            channel.QueueBind($"myheadersqueue{i}", "myheadersexchange", string.Empty, map);

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (sender, e) =>
            {
                Console.WriteLine(Encoding.UTF8.GetString(e.Body.ToArray()));
            };

            channel.BasicConsume($"myheadersqueue{i}", true, consumer);

            Console.WriteLine($"consumer{i} started");

        }
    }
}
